package com.feng.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.reggie.common.R;
import com.feng.reggie.dto.DishDto;
import com.feng.reggie.entity.Category;
import com.feng.reggie.entity.Dish;
import com.feng.reggie.entity.DishFlavor;
import com.feng.reggie.service.CategoryService;
import com.feng.reggie.service.DIshService;
import com.feng.reggie.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */

@RestController
@Slf4j
@RequestMapping("/dish")

public class DishController {
    @Autowired
    private DIshService dIshService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.debug(dishDto.toString());
        dIshService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品信息的分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器对象
        Page<Dish> pageinfo=new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage=new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        if(name!=null){
            queryWrapper.like(Dish::getName,name);
        }
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行分页查询
        dIshService.page(pageinfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageinfo,dishDtoPage,"records");
        List<Dish> records = pageinfo.getRecords();
        List<DishDto> list=records.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            //对应每个菜品的分类id
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                String categoryName = byId.getName();
                dishDto.setCategoryName(categoryName );
            }

            return dishDto;
        }).collect(Collectors.toList());



        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);



    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto byidWithFlavor = dIshService.getByidWithFlavor(id);
        return R.success(byidWithFlavor);
    }

    /**
     * 菜品修改的提交
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dIshService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功！");
    }

    /**
     * 删除和批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String[] ids){
        for (String id : ids) {
            dIshService.removeById(id);
        }

        return R.success("菜品删除成功！");
    }

    /**
     * 修改菜品起售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> sale(@PathVariable int status,String[] ids){
        for (String id : ids) {
            Dish dish = dIshService.getById(id);
            dish.setStatus(status);
            dIshService.updateById(dish);
        }
        return R.success("修改状态成功！");
    }

    /**
     * 根据条件查询对应的菜品
     * @param dish
     * @return
     */
//    @GetMapping("/list")
//    public R<List> list(Dish dish){
//        //构造查询条件对象
//        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        //添加排序调教
//
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        //添加条件，查询状态为1（起售状态）的菜品
//        queryWrapper.eq(Dish::getStatus,1);
//        List<Dish> list = dIshService.list(queryWrapper);
//
//
//        return R.success(list);
//    }

    /**
     * 根据id查询菜品的信息，需查询两张表，dish dish_flavor表
     * @param dish
     * @return
     * 返回dish表信息和口味信息
     *
     */
  @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //构造查询条件对象
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //添加排序调教

        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dIshService.list(queryWrapper);
        List<DishDto> dishDtos=list.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            //当前菜品的id
            Long dishid=item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishid);
            List<DishFlavor> list1 = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtos);


    }


}
